﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

using BeachRunner.Utility;

namespace BeachRunner.Menu
{
    /// <summary>
    /// Ein Menübutton
    /// 
    /// Autor: Arne Blumentritt, OLD (Mattis Hänel)
    /// </summary>
    class Button
    {
        /// <summary>
        /// Ein SpriteSet als Buttongrundlage
        /// </summary>
        private Texture2D texture;

        /// <summary>
        /// Der sensitive Bereich
        /// </summary>
        private Rectangle rectangle;

        /// <summary>
        /// Der sensitive Bereich
        /// </summary>
        private Vector2 position;

        /// <summary>
        /// Name des Buttons
        /// </summary>
        private String name;

        /// <summary>
        /// Schriftart des Buttons
        /// </summary>
        private SpriteFont spriteFont;

        public Vector2 Position
        {
            get { return position; }
            set 
            {
                if (texture != null)
                {
                    rectangle = new Rectangle((int)value.X, (int)value.Y, texture.Width, texture.Height);
                    this.position = value;
                } 
            }
        }

        /// <summary>
        /// Wurde der Button geklickt
        /// </summary>
        private bool isClicked;

        /// <summary>
        /// Erstellt einen neuen Button
        /// </summary>
        /// <param name="position">Die Position des Buttons</param>
        /// <param name="spriteSet">Das zu verwendende SpriteSet für den Hitnergrund</param>
        public Button(Vector2 position, Texture2D texture)
        {
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            this.texture = texture;
            this.position = position;
        }

        /// <summary>
        /// Erstellt einen neuen Button mit Text
        /// Autor: Arne Blumentritt
        /// </summary>
        /// <param name="position">Die Position des Buttons</param>
        /// <param name="spriteSet">Das zu verwendende SpriteSet für den Hitnergrund</param>
        /// <param name="name">Der Name des Buttons</param>
        /// <param name="spriteFont">Schriftart des Buttons</param>
        public Button(Vector2 position, Texture2D texture, String name, SpriteFont spriteFont)
        {
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            this.texture = texture;
            this.position = position;
            this.name = name;
            this.spriteFont = spriteFont;
        }

        /// <summary>
        /// Die Logik des Buttons
        /// </summary>
        public void Update()
        {
            if (IOTouchControl.IsTouchHit(rectangle))
            {
                this.isClicked = true;
            }
            else
            {
                this.isClicked = false;
            }
        }

        /// <summary>
        /// Zeichnet den Button
        /// </summary>
        /// <param name="spriteBatch">Das SpriteSet auf dem der Button gezeichnet werden soll</param>
        public void Draw( SpriteBatch spriteBatch )
        {
            spriteBatch.Draw(texture, position, Color.White);

            // Zeichnet einen Text zentriert auf einem Button
            // Autor: Arne Blumentritt
            if (name != null)
                spriteBatch.DrawString(spriteFont, name,
                    new Vector2((float)Math.Round(Position.X + (texture.Width - spriteFont.MeasureString(name).X) / 2), (float)Math.Round(Position.Y + (texture.Height - spriteFont.MeasureString(name).Y) / 2)), Color.White);
        }

        /// <summary>
        /// Hat der Button ausgelöst?
        /// </summary>
        public bool IsClicked
        {
            get { return isClicked; }
            set { isClicked = value; }
        }
    }
}
